package com.lebang.service;

import com.lebang.domain.DownloadMemberRecord;
import com.lebang.mapper.DownloadMemberRecordMapper;
import com.lebang.mapper.LbbMemberTradeMapper;
import com.lebang.mapper.MemberPointRecordMapper;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.enums.buss.DownCategory;
import com.lebang.model.enums.buss.DownReword;
import com.lebangweb.common.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class HundredAwardsService {
    @Autowired
    private DownloadMemberRecordMapper downloadMemberRecordMapper;
    @Autowired
    private DownloadMemberRecordMapper recordMapper;
    @Autowired
    private LbbMemberTradeMapper lbbMemberTradeMapper;
    @Autowired
    private MemberPointRecordMapper memberPointRecordMapper;

    /**
     * 百倍奖励任务填充
     */
    public int updateRecord(RequestUserInfo userInfo, Integer status, DownloadMemberRecord record, String appName, String pkgName) {
        int num ;
        if (status == 2) {
            //非首次
            record.setHasFirst(2);
            if (record.getType().equals(DownCategory.DOWN_LOAD.getIndex())) {
                //下载型
                record.setCategory(DownReword.DOWN_UN_FIRST.getIndex());

            } else {
                //浏览型
                record.setCategory(DownReword.NONE_IN.getIndex());
            }
            num = 2;
        } else {
            //查询首次还是第二次
            DownloadMemberRecord roc = this.selectByAppNameAndMemberId(appName, userInfo.getMemberId(), pkgName);
            if (roc == null) {
                //首次
                record.setHasFirst(1);
                if (record.getType().equals(DownCategory.DOWN_LOAD.getIndex())) {
                    //下载型
                    record.setCategory(DownReword.DOWN_FIRST.getIndex());
                } else {
                    //浏览型
                    record.setCategory(DownReword.NONE_IN.getIndex());
                }
                num = 1;
            } else {
                record.setHasFirst(2);
                //非首次
                if (record.getType().equals(DownCategory.DOWN_LOAD.getIndex())) {
                    //下载型
                    record.setCategory(DownReword.DOWN_UN_FIRST.getIndex());

                } else {
                    //浏览型
                    record.setCategory(DownReword.NONE_IN.getIndex());
                }
                num = 2;
            }
        }
        record.setAppName(appName);
        record.setPkgName(pkgName);
        downloadMemberRecordMapper.updateByPrimaryKeySelective(record);
        return num;
    }

    /**
     * 查询有没有做过包名应用名称
     *
     * @param appName
     * @param memberId
     * @param pkgName
     * @return
     */
    public DownloadMemberRecord selectByAppNameAndMemberId(String appName, String memberId, String pkgName) {
        if (TextUtil.isEmpty(pkgName)) {
            return recordMapper.selectByAppNameAndMemberId(appName, memberId, null, null, null);
        } else {
            return recordMapper.selectByAppNameAndMemberId(null, memberId, pkgName, null, null);
        }
    }

    /**
     * 获取今日用户收益
     */
    public BigDecimal getTodayMoney(String memberId) {
        LocalDateTime star = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        BigDecimal money = lbbMemberTradeMapper.selectMoneyByTime(memberId, star, end);
        BigDecimal point = memberPointRecordMapper.selectPointByTime(memberId, star, end);
        return money.add(point).setScale(1, BigDecimal.ROUND_DOWN);
    }
}
